// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

const {ccclass, property} = cc._decorator;

@ccclass
export default class DataUtil {

    static convertToArray<T>(any: any) : T[] {
        var array = any;
        var ret = new Array<T>();
        for (let index = 0; index < array.length; index++) {
            const element = array[index];
            var item = Object.assign({}, element) as T;
            ret[index] = item;
        }
        return ret;
    }
    static shallowCopy<T>(obj: T): T {
        return Object.assign({}, obj);
    }
    
    static convertToType<T>(value: any): T {
        return Object.assign({}, value) as T; // 泛型类型断言
    }

    static combineData(obj1, obj2) {
        let new1 = Object.assign({}, obj1);
        for (const key in obj2) {
            if (obj2.hasOwnProperty(key) && !obj1.hasOwnProperty(key)) {
                new1[key] = obj2[key];
            }
        }
        return new1;
    }

    static formatNumberStr(value: number): string {
        if (value == null) return "null";
        let value1 = value.toFixed(2);
        if (value > 1000000000000) {
            return (value / 1000000000000).toFixed(2) + "万亿";
        } else if (value > 1000000000) {
            return (value / 1000000000).toFixed(2) + "亿";
        } else if (value > 10000) {
            return (value / 10000).toFixed(2) + "万";
        } else {
            return value1;
        }
    }

    static formatNumberWStr(value: number): string {
        if (value == null) return "null";
        let value1 = value.toFixed(2);
        if (value > 100000000) {
            return (value / 100000000).toFixed(2) + "万亿";
        } else if (value > 10000) {
            return (value / 10000).toFixed(2) + "亿";
        } else {
            return value1 + "万";
        }
    }

    static formatNumberBStr(value: number): string {
        if (value == null) return "null";
        let value1 = value.toFixed(2);
        return value1 + "%";
    }

    

    // update (dt) {}
}
